<?php


namespace app\common\exception;


class Curl
{
    /**
     * get 请求
     * @param $url
     * @param array $params
     * @return bool|string
     */
    public static function curlGet($url, $params = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //ssl
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在

        if (!empty($params)) {
            curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
        } else
            curl_setopt($ch, CURLOPT_URL, $url);

        $response = curl_exec($ch);

        if ($response === FALSE) {
            $err = curl_error($ch);
            curl_close($ch);
            return false;
        } else {
            curl_close($ch);
            return $response;
        }
    }

    public static function curlPost($url, $params = [], $isJson = false)
    {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        if ($isJson) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params, JSON_UNESCAPED_UNICODE));
        } else
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));

        $response = curl_exec($ch);

        if ($response === FALSE) {
            $err = curl_error($ch);
            curl_close($ch);
            return false;
        } else {
            curl_close($ch);
            return $response;
        }
    }
}